# ============================================================
# Economic Crimes by Category in Latin America (2020-2023)
# Reproducible R Analysis Script
# Author: de la Serna, Juan Moises
# DOI: https://doi.org/10.7910/DVN/8FXZOJ
# Date: 2026-03-20
# ============================================================

# ---- 0. Setup ----
required_packages <- c("ggplot2", "dplyr", "tidyr", "readr", "scales", "viridis")
new_packages <- required_packages[!(required_packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)

library(ggplot2); library(dplyr); library(tidyr)
library(readr); library(scales); library(viridis)

# ---- 1. Load Data ----
df <- read_tsv("delitos_economicos_latinoamerica_2020_2023.tab")
df2 <- read_tsv("delitos_economicos_latinoamerica_2020_2023-2.tab")

cat("Countries:", length(unique(df$country)), "\n")
cat("Years:", paste(sort(unique(df$year)), collapse=", "), "\n")
cat("Observations:", nrow(df), "\n")

# ---- 2. Figure 1: CPI by Country 2023 ----
df_2023 <- df %>% filter(year == 2023, !is.na(cpi_score)) %>%
  arrange(cpi_score) %>% mutate(country = factor(country, levels = country))

fig1 <- ggplot(df_2023, aes(x = country, y = cpi_score, fill = cpi_score)) +
  geom_col() + scale_fill_viridis_c(option = "D", direction = -1) +
  coord_flip() +
  labs(title = "Corruption Perceptions Index in Latin America, 2023",
       x = NULL, y = "CPI Score (0-100)",
       caption = "Source: Transparency International | de la Serna (2026)") +
  theme_minimal()
ggsave("fig1_cpi_by_country_2023.png", fig1, width=10, height=7, dpi=300)

# ---- 3. Figure 2: CPI Trend 2020-2023 ----
countries_sel <- c("Chile","Uruguay","Argentina","Brazil","Mexico","Venezuela","Haiti")
fig2 <- df %>% filter(country %in% countries_sel, !is.na(cpi_score)) %>%
  ggplot(aes(x=year, y=cpi_score, color=country, group=country)) +
  geom_line(size=1.2) + geom_point(size=3) +
  scale_color_viridis_d() +
  labs(title="CPI Trend in Selected Countries (2020-2023)",
       x="Year", y="CPI Score", caption="de la Serna (2026)") +
  theme_minimal()
ggsave("fig2_cpi_trend.png", fig2, width=10, height=6, dpi=300)

# ---- 4. Figure 3: Tax Evasion as % GDP ----
fig3 <- df %>% filter(year==2023, !is.na(tax_evasion_gdp_pct)) %>%
  arrange(desc(tax_evasion_gdp_pct)) %>%
  mutate(country = factor(country, levels=country)) %>%
  ggplot(aes(x=country, y=tax_evasion_gdp_pct, fill=tax_evasion_gdp_pct)) +
  geom_col() + scale_fill_gradient(low="#ffffcc", high="#d73027") +
  coord_flip() +
  labs(title="Tax Evasion as % of GDP in Latin America, 2023",
       x=NULL, y="Tax Evasion (% GDP)", caption="Source: GFI/IDB | de la Serna (2026)") +
  theme_minimal()
ggsave("fig3_tax_evasion.png", fig3, width=10, height=7, dpi=300)

# ---- 5. Figure 4: Money Laundering Risk Distribution ----
ml_colors <- c("Low"="#1a9850","Medium"="#fee08b","High"="#f46d43","Very High"="#d73027")
fig4 <- df %>% filter(!is.na(ml_risk_level)) %>%
  mutate(ml_risk_level = factor(ml_risk_level, levels=names(ml_colors))) %>%
  count(ml_risk_level) %>%
  ggplot(aes(x=ml_risk_level, y=n, fill=ml_risk_level)) +
  geom_col() + scale_fill_manual(values=ml_colors) +
  labs(title="Money Laundering Risk Distribution (2020-2023)",
       x="Risk Level", y="Country-Year Observations",
       caption="Source: FATF/GAFI | de la Serna (2026)") +
  theme_minimal() + theme(legend.position="none")
ggsave("fig4_ml_risk.png", fig4, width=8, height=6, dpi=300)

# ---- 6. Figure 5: Fraud Rate vs CPI ----
fig5 <- df %>% filter(year==2023, !is.na(cpi_score), !is.na(fraud_companies_pct)) %>%
  ggplot(aes(x=cpi_score, y=fraud_companies_pct, label=country)) +
  geom_point(aes(color=fraud_companies_pct), size=4) +
  geom_text(vjust=-0.8, size=3) +
  scale_color_viridis_c(option="B", direction=-1) +
  labs(title="Fraud Rate vs CPI in Latin America, 2023",
       x="CPI Score (0-100)", y="Companies Affected (%)",
       caption="Sources: PwC + TI | de la Serna (2026)") +
  theme_minimal()
ggsave("fig5_fraud_vs_cpi.png", fig5, width=10, height=7, dpi=300)

cat("All 5 figures saved successfully.\n")